package vben.base.sys.org.rece;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import vben.common.jdbc.sqler.Isqler;
import vben.common.jdbc.sqler.JdbcHelper;

import java.util.ArrayList;
import java.util.List;

@Component
@RequiredArgsConstructor
public class SysOrgReceDao {

    public void saveAll(List<SysOrgRece> list) {
        List<Object[]> deleteList = new ArrayList<>();
        List<Object[]> insertLIst = new ArrayList<>();
        for (SysOrgRece rece : list) {
            Object[] arr = new Object[2];
            arr[0] = rece.getUid();
            arr[1] = rece.getOid();
            deleteList.add(arr);

            Object[] arr2 = new Object[4];
            arr2[0] = rece.getId();
            arr2[1] = rece.getUid();
            arr2[2] = rece.getOid();
            arr2[3] = rece.getUptim();
            insertLIst.add(arr2);
        }
        String deleteSql = "delete from sys_org_rece where uid=? and oid=?";
        jdbcHelper.batch(deleteSql, deleteList);
        String insertSql = "insert into sys_org_rece(id,uid,oid,uptim) values(?,?,?,?)";
        jdbcHelper.batch(insertSql, insertLIst);
    }


    public void insert(SysOrgRece rece) {
        Isqler sqler = new Isqler("sys_org_rece");
        sqler.add("id", rece.getId());
        sqler.add("name", rece.getUid());
        sqler.add("depid", rece.getOid());
        sqler.add("tier", rece.getUptim());
        jdbcHelper.getTp().update(sqler.getSql(), sqler.getParams());
    }

    public void deleteById(String id) {
        String sql = "delete from sys_org_rece where id=?";
        jdbcHelper.getTp().update(sql, id);
    }

    private final JdbcHelper jdbcHelper;
}
